package com.btex.app.treaty;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.btex.app.BaseActivity;
import com.btex.app.Config;
import com.btex.app.MyApplication;
import com.btex.app.R;
import com.btex.app.adapter.MyFragmentAdapter;
import com.btex.app.entity.FuturesListInfo;
import com.btex.app.tools.CookieTool;
import com.btex.app.tools.SharedPreferencesUtil;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.util.PreferencesCookieStore;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CounterCorrectActivity extends BaseActivity {


    @BindView(R.id.activity_counter_viewpager)
    ViewPager mActivityCounterViewpager;
    @BindView(R.id.activity_counter_imag_back)
    ImageView mActivityCounterImagBack;
    @BindView(R.id.activity_counter_tv_shouyi)
    TextView mActivityCounterTvShouyi;
    @BindView(R.id.activity_counter_tv_mubiao)
    TextView mActivityCounterTvMubiao;
    @BindView(R.id.activity_counter_tv_qiangping)
    TextView mActivityCounterTvQiangping;
    private String mData = "10";
    public List<FuturesListInfo.DataBean> mData1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_correct_counter);
        ButterKnife.bind(this);
        getMxCcontracts();
        initViewPager();
        mActivityCounterTvShouyi.setSelected(true);

    }

    private void initViewPager() {
        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(new ShouYiFragment());
        fragments.add(new MuBiaoFragment());
        fragments.add(new QiangPingFragment());
        ArrayList<String> strings = new ArrayList<>();
        strings.add("收益计算");
        strings.add("目标价格");
        strings.add("强平价格");

        mActivityCounterViewpager.setAdapter(new MyFragmentAdapter(getSupportFragmentManager(), fragments, strings));
        mActivityCounterViewpager.setOffscreenPageLimit(3);
        mActivityCounterViewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                if (position == 0) {

                    setState();
                    mActivityCounterTvShouyi.setSelected(true);

                } else if (position == 1) {
                    setState();
                    mActivityCounterTvMubiao.setSelected(true);
                } else {
                    setState();
                    mActivityCounterTvQiangping.setSelected(true);
                }

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void setData() {

    }


    @Override
    public void onClick(View v) {

    }

    @OnClick({R.id.activity_counter_imag_back, R.id.activity_counter_tv_shouyi, R.id.activity_counter_tv_mubiao, R.id.activity_counter_tv_qiangping})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.activity_counter_imag_back:
                finish();
                break;
            case R.id.activity_counter_tv_shouyi:
                mActivityCounterViewpager.setCurrentItem(0, false);
                break;
            case R.id.activity_counter_tv_mubiao:
                mActivityCounterViewpager.setCurrentItem(1, false);
                break;
            case R.id.activity_counter_tv_qiangping:
                mActivityCounterViewpager.setCurrentItem(2, false);
                break;
        }
    }


    public void setState() {
        mActivityCounterTvShouyi.setSelected(false);
        mActivityCounterTvMubiao.setSelected(false);
        mActivityCounterTvQiangping.setSelected(false);
    }


    private void getMxCcontracts() {


        SharedPreferencesUtil sharedPreferences = SharedPreferencesUtil.getSharedPreferences(this);
        PreferencesCookieStore cookieStore = CookieTool.getCookieStore(this, sharedPreferences);
        RequestParams params = new RequestParams();
        params.addHeader("app-version", Config.VERSION);
        params.addHeader("channel", MyApplication.mApp.getApplicationMetaData("CXD_CHANNEL_ID"));
        params.addHeader("myclient", "android");

        int num = (int) ((Math.random() * 9 + 1) * 100000);
        new HttpUtils().configCookieStore(cookieStore).send(HttpRequest.HttpMethod.GET, Config.GET_MX_CONTRACTS + "?rnd=" + String.valueOf(num), params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {

                Log.i("eruheywfhyw", responseInfo.result);

                try {

                    JSONObject jsonObj = new JSONObject(responseInfo.result);
                    if (jsonObj.getString("code").equals("1")) {

//                        setList(responseInfo.result);
                        Gson gson = new Gson();
                        FuturesListInfo mFuturesInfo   = gson.fromJson(responseInfo.result, FuturesListInfo.class);
                        mData1 = mFuturesInfo.getData();


                    } else {

                    }


                } catch (JSONException e) {
                    e.printStackTrace();

                }


            }

            @Override
            public void onFailure(HttpException error, String msg) {

            }

        });

    }
}
